package top.blackr.folivora.common.security.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import top.blackr.folivora.common.security.auth.LoginAuthentication;
import top.blackr.folivora.common.security.auth.SecUser;

@Slf4j
@Component
public class LoginListener {

    @Async
    @EventListener
    public void loginBefore(LoginBeforeEvent event) {
        LoginAuthentication authentication = event.getAuthentication();
        log.info("用户 {} 正在通过 {} 登陆", authentication.getCredential(), authentication.getLoginType());
    }

    @Async
    @EventListener
    public void loginAfter(LoginAfterEvent event) {
        SecUser user = event.getUser();
        LoginAuthentication authentication = event.getAuthentication();
        log.info("用户 {} 登陆成功，登陆id:{}", authentication.getCredential(), user.getUserId());
    }
}
